package ar.com.binarit

import grails.validation.ValidationException
import ar.com.binarit.entities.Categoria
import ar.com.binarit.entities.TipoEvento

class NuevaMubbiaController extends AbstractMubbiaController {
	
	def mailService
	
    def index() { 
		
		if (!springSecurityService.isLoggedIn()){
			render(view: "/login/mustLogin")
		}
		
		def mubbiaInstance = llenarDatosMubbia(params, true)
		if (!mubbiaInstance.puedeEditarMubbia(springSecurityService.currentUser)){
			redirect(action: "show", params: params)
		}
		def result = [mubbiaInstance: mubbiaInstance]
		result.putAll(getMapaDatos(mubbiaInstance))
		result
	}
	
	def show() {
		log.warn "show: ${params}"
		redirect(controller:"mubbias", action: "show", params:params)	
	}
	
	def editarMubbia = {
		def result
		def mubbiaInstance
		try{
			mubbiaInstance = llenarDatosMubbia(params, true)
			result = mubbiaService.validarMubbia(mubbiaInstance)
			if (result){
				mubbiaService.guardarMubbia(mubbiaInstance)
			}
		}catch (ValidationException e) {
			result = false
		}
		mostrarResultados(result, "Mubbia correcta", mubbiaInstance)
	}  
	
	def previsualizarMubbia = {
		def result
		def mubbiaInstance
		try{
			mubbiaInstance = llenarDatosMubbia(params, true)
			result = mubbiaService.validarMubbia(mubbiaInstance)
			if (result){
				mubbiaService.guardarMubbia(mubbiaInstance)
			}
		}catch (ValidationException e) {
			result = false
		}
		mostrarResultados(result, "Mubbia correcta", mubbiaInstance)
	}
	
	def retirarMubbia = {
		def result = true
		try{
			def mubbiaInstance = mubbiaService.obtenerMubbia(params.mubbiaId)
			if (mubbiaInstance){
				mubbiaService.retirarMubbia(mubbiaInstance)
			}
		}catch (ValidationException e) {
			result = false
		}
		redirect(action: "show", params:[mubbiaId:params?.mubbiaId])
	}
	
	def cerrarMubbia = {
		def result = true
		try{
			def mubbiaInstance = mubbiaService.obtenerMubbia(params.mubbiaId)
			if (mubbiaInstance){
				result = mubbiaService.cerrarMubbia(mubbiaInstance)
				if (result){
					def mapaInfo = [mubbia: mubbiaInstance, mails: mubbiaInstance.usuariosParticipantes]
					mubbiaService.envioMail(TipoEvento.cierraMubbia(), mapaInfo)
				}
			}
		}catch (ValidationException e) {
			result = false
		}
		redirect(action: "show", params:[mubbiaId:params?.mubbiaId])
	}
	
	
	def unirseAMubbia = {
		def result = true
		try{
			def mubbiaInstance = mubbiaService.obtenerMubbia(params.mubbiaId)
			if (mubbiaInstance){
				def Person = Person.findById(springSecurityService.currentUser?.id)
				mubbiaService.unirseAMubbia(mubbiaInstance, Person)
			}
		}catch (ValidationException e) {
			result = false
		}
		redirect(action: "show", params:[mubbiaId:params?.mubbiaId])
	}
	
	def desunirseAMubbia = {
		def result = true
		try{
			def mubbiaInstance = mubbiaService.obtenerMubbia(params.mubbiaId)
			if (mubbiaInstance){
				def Person = Person.findById(springSecurityService.currentUser?.id)
				mubbiaService.desunirseAMubbia(mubbiaInstance, Person)
			}
		}catch (ValidationException e) {
			result = false
		}
		redirect(action: "show", params:[mubbiaId:params?.mubbiaId])
	}
	
	def publicarMubbia = {
		def result = true
		try{
			def mubbiaInstance = mubbiaService.obtenerMubbia(params.mubbiaId)
			if (mubbiaInstance){
				mubbiaService.publicarMubbia(mubbiaInstance)
				def mapaInfo = [mubbia: mubbiaInstance, mails: [mubbiaInstance.usuarioCreador]]
				mubbiaService.envioMail(TipoEvento.nuevaMubbia(), mapaInfo)
			}
		}catch (ValidationException e) {
			result = false
		}
		redirect(action: "show", params:[mubbiaId:params?.mubbiaId])
	}
	
//	def dibujarUbicaciones = {
//		println "paso por dibujar"
//		render(template: "/templates/seleccionarUbicacion")
//	}
	
//	def enviarEmailNuevaMubbia (mubbia) {
//		mailService.sendMail {
//			to "${mubbia.usuarioCreador.email}"
//			from "mubbiacom@gmail.com"
//			subject "Creaste la Mubbia: ${mubbia.titulo}"
//			html mubbia.descripcion
//		 }
//	}
	
	def getMapaDatos(mubbiaInstance){
		[categorias:Categoria.all.sort{it.descripcion},
		tags : mubbiaInstance?.tags?.toString().replace('[', '').replace(']', '')] + super.getMapaDatos(mubbiaInstance)
	}
	
}
